<template>
	<view class="content">
		<!-- <block v-if="!isphone">
			<image class="heard" src="/static/logo.png" mode=""></image>
			<view class="title">
				获取你的手机号
			</view>
			<view class="split-line"></view>
			<view class="auth">
				登录后开发者将获取一些权限
			</view>
			<view class="phone">
				·获取你的手机号
			</view>
			<button class="phone-button" open-type="getPhoneNumber" @getphonenumber='getPhoneNumber'>微信授权登录</button>
		</block> -->
		<block>
			<image class="heard" src="https://www.szzn.group/uploads/20221128/d68d8535bb12486341a933ad849a86d1.png" mode=""></image>
			<view class="title">获取你的头像信息</view>
			<view class="split-line"></view>
			<view class="auth">登录后开发者将获取一些权限</view>
			<view class="phone">·获取你的头像信息</view>
			<button class="login-button" @click="wxGetUserInfo" style="padding: 0 180rpx;"><view class="">微信授权登录</view></button>
		</block>
	</view>
</template>

<script>
import { login } from '@/api/api.js';
export default {
	components: {},
	data() {
		return {
			isphone: false,
			phone: '',
			image: '',
			nickName: '',
			code: '',
			falg: ''
		};
	},
	onShow() {},
	onLoad() {
		let that = this;
		uni.login({
			provider: 'weixin',
			success: function(red) {
				uni.setStorageSync('code', red.code);
				that.code = red.code;
			}
		});
	},
	methods: {
		// 获取手机号
		// getPhoneNumber(e) {
		// 	this.phone = e.detail.code
		// 	uni.setStorageSync('phone', e.detail.code);
		// 	this.isphone = true
		// },
		wxGetUserInfo() {
			let that = this;
			if (that.falg) return;
			uni.getUserProfile({
				desc: '登录后可同步数据',
				lang: 'zh_CN',
				success: function(infoRes) {
					uni.setStorageSync('userInfo', infoRes.userInfo); //记录是否第一次授权  false:表示不是第一次授权
					uni.showLoading({
						title: '加载中'
					});
					that.login(infoRes);
					that.falg = true;
					let time = setTimeout(() => {
						that.falg = false;
						clearTimeout(time);
					}, 5000);
				},
				fail(res) {}
			});
		},
// asd
		login(info) {
			info.code = this.code;
			if (uni.getStorageSync('superiorId') != '' || uni.getStorageSync('superiorId') != undefined || uni.getStorageSync('superiorId') != null) {
				info.spread_spid = uni.getStorageSync('superiorId');
			} else {
				info.spread_spid = 0;
			}
			login(info)
				.then(res => {
					uni.setStorageSync('userInfotow', res.data);
					uni.setStorageSync('userId', res.data.userId);
					uni.setStorageSync('ifStore', res.data.ifstore);
					uni.setStorageSync('storeId', res.data.ifstore);
					uni.setStorageSync('token', res.data.data.token);
					if (res.data.code == '200') {
						uni.hideLoading();
						this.getPageNum();
					} else {
						uni.hideLoading();
						uni.showToast({
							title: '登录异常',
							duration: 1000,
							icon:'error'
						});
					}
				})
				.catch(res => {
					console.log('错了', res);
				});
		},

		getPageNum() {
			var pages = getCurrentPages(); //获取加载的页面
			var currentPage = pages[pages.length - 2]; // index-detail页面
			if(currentPage == undefined){
				uni.showToast({
					title: '登录成功',
					duration: 1000,
				});
				let time = setTimeout(function() {
					uni.reLaunch({
						url:'/pages/home/index/index'
					 });
					clearTimeout(time);
				}, 1000);
				return
			}
			var url = currentPage.route; //当前页面url
			uni.showToast({
				title: '登录成功',
				duration: 1000,
			});
			let time = setTimeout(function() {
				uni.navigateBack({ url });
				clearTimeout(time);
			}, 1000);
		},

		wxlogin(info) {
			uni.login({
				provider: 'weixin',
				success: function(red) {
					uni.setStorageSync('code', red.code);
				}
			});
		}
	}
};
</script>

<style lang="scss">
.content {
	width: 100vw;
	height: 100vh;
	overflow: hidden;
	position: relative;

	.heard {
		display: block;
		width: 196rpx;
		height: 196rpx;
		// background-color: #f4f4f5;
		border-radius: 50%;
		margin: 304rpx auto 0;
	}

	.title {
		width: 100%;
		height: 40rpx;
		font-size: 24rpx;
		text-align: center;
		font-weight: 400;
		color: #333;
		font-family: PingFang SC;
		margin-top: 50rpx;
	}

	.split-line {
		width: 90%;
		height: 0rpx;
		border: 1rpx solid #dadada;
		margin: 50rpx auto 0;
	}

	.auth {
		width: 100%;
		height: 44rpx;
		padding-left: 58rpx;
		margin-top: 30rpx;
		font-size: 28rpx;
		color: #000;
		line-height: 44rpx;
		font-weight: bold;
	}

	.phone {
		width: 100%;
		height: 40rpx;
		font-size: 28rpx;
		color: #b8b8b8;
		margin-top: 10rpx;
		padding-left: 78rpx;
	}

	.phone-button {
		display: block;
		width: 632rpx;
		height: 94rpx;
		background-color: #07c160;
		border-radius: 14rpx;
		margin: 70rpx auto 0;
		text-align: center;
		line-height: 94rpx;
		color: #fff;
		letter-spacing: 5rpx;
	}

	.login-button {
		display: flex;
		justify-content: center;
		align-items: center;
		width: 632rpx;
		height: 94rpx;
		background-color: #07c160;
		border-radius: 14rpx;
		margin: 150rpx auto 0;
		text-align: center;
		line-height: 94rpx;
		color: #fff;
		letter-spacing: 5rpx;

		image {
			margin-left: 25%;
			margin-right: 20rpx;
			display: inline-block;
			width: 100rpx;
			height: 100rpx;
		}
	}
}
</style>
